package com.oba.king.controlmanage;

import java.util.List;

import com.oba.base.dao.BaseDao;
import com.oba.base.interfaces.IHsqlBuilder;
import com.oba.king.datalib.CompanyInfo;
import com.oba.king.datalib.Customer;
import com.oba.king.datalib.CustomerCondition;
import com.oba.king.datalib.creditcontrol.CreditControl;
import com.oba.king.login.UserInfo;
import com.oba.king.sysmanager.area.Area;

public class ProxyContractDao extends BaseDao{
	
	/**
	 * 查找所有的协议信息
	 * @return
	 */
		public IHsqlBuilder findAll() {
			return getSqlBuilder("from ProxyContract p order by p.contractId");
		}
		public IHsqlBuilder findNeedAudit() {
			IHsqlBuilder sql = getSqlBuilder("from ProxyContract p");
			sql.add("p.auditingStatus in (1,4)");
			sql.addend("order by p.contractId");
			return sql;
			
		}

	public IHsqlBuilder findByProxyContract(ProxyContractCondition proxyContractCondition) {
		IHsqlBuilder sql = getSqlBuilder("from ProxyContract p");
		sql.add("p.contractId = ?", proxyContractCondition.getContractId());
		sql.add("p.archiveNumber = ?", proxyContractCondition.getArchiveNumber());
		sql.add("p.idCardNumber = ?", proxyContractCondition.getIdCardNumber());
		sql.add("p.startDate > ?",proxyContractCondition.getStartDate());
		sql.add("p.endDate < ?",proxyContractCondition.getEndDate());
		sql.add("p.givenArea like ?",proxyContractCondition.getGivenArea(),"%","%");	
		sql.add("p.givenMarket like ?",proxyContractCondition.getGivenMarket(),"%","%");
		if(proxyContractCondition!=null&&!(proxyContractCondition.getAuditingStatus()==0)){
		sql.add("p.auditingStatus = ?",proxyContractCondition.getAuditingStatus());
		}
		sql.add("p.customer = ?",proxyContractCondition.getCustomer());
		sql.addend("order by p.contractId");
		return sql;

	}
	
	public IHsqlBuilder findAuditProxyContract(ProxyContractCondition proxyContractCondition) {
		IHsqlBuilder sql = getSqlBuilder("from ProxyContract p");
		sql.add("p.auditingStatus in (1,4)");
		sql.add("p.contractId = ?", proxyContractCondition.getContractId());
		sql.add("p.archiveNumber = ?", proxyContractCondition.getArchiveNumber());
		sql.add("p.idCardNumber = ?", proxyContractCondition.getIdCardNumber());
		sql.add("p.startDate > ?",proxyContractCondition.getStartDate());
		sql.add("p.endDate < ?",proxyContractCondition.getEndDate());
		sql.add("p.givenArea like ?",proxyContractCondition.getGivenArea(),"%","%");	
		sql.add("p.givenMarket like ?",proxyContractCondition.getGivenMarket(),"%","%");
		if(proxyContractCondition!=null&&!(proxyContractCondition.getAuditingStatus()==0)){
		sql.add("p.auditingStatus = ?",proxyContractCondition.getAuditingStatus());
		}
		sql.add("p.customer = ?",proxyContractCondition.getCustomer());
		sql.addend("order by p.contractId");
		return sql;

	}
	
	
	public List<Customer> findAllCustomer(){
		
		return find("from Customer c order by c.customerName");
		
	}
	
	/**
	 * 查找本公司信息 先随便查出来一个
	 * @return
	 */
	public List<CompanyInfo> findAllCompany(){
		
		return find("from CompanyInfo c order by c.companyName");
		
	}
	
	private final static String DEL_PRODUCT_DETAILS = "delete from PROXYPRODUCTDETAILS WHERE CONTRACT_ID = ";
	
	public void deleteContract(ProxyContract proxyContract){
		jdao.execute(DEL_PRODUCT_DETAILS+proxyContract.getContractSequnceId());
		delete(proxyContract);
	}
	
	
}
